<?php

/*
 * This file is part of the symfony package.
 * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * sfWidgetFormDate represents a date widget.
 *
 * @package    symfony
 * @subpackage widget
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
 * @version    SVN: $Id: nlWidgetFormHtml5Input.class.php 392 2010-12-06 12:49:28Z sn $
 */
class nlWidgetFormHtml5Input extends sfWidgetFormInputText {

    /**
     * @param array $options An array of options
     * @param array $attributes An array of default HTML attributes
     *
     * @see sfWidgetForm
     */
    protected function configure($options = array(), $attributes = array()) {
        parent::configure($options, $attributes);

        $this->addOption('required', false);
        $this->addOption('pattern');
        $this->addOption('type', 'text');

    }

    /**
     * Returns true if the widget is hidden.
     *
     * @return Boolean true if the widget is hidden, false otherwise
     */
    public function isRequired() {
        return $this->getOption('required');
    }

    /**
     * Returns true if the widget is hidden.
     *
     * @return Boolean true if the widget is hidden, false otherwise
     */
    public function getPattern() {
        return $this->getOption('pattern');
    }

    /**
     * Sets the hidden flag for the widget.
     *
     * @param bool $boolean  true if the widget must be hidden, false otherwise
     *
     * @return sfWidget The current widget instance
     */
    public function setRequired($boolean) {
        $this->setOption('required', (boolean) $boolean);

        return $this;
    }

    /**
     * Renders the widget.
     *
     * @param  string $name        The element name
     * @param  string $value       The value displayed in this widget
     * @param  array  $attributes  An array of HTML attributes to be merged with the default HTML attributes
     * @param  array  $errors      An array of errors for the field
     *
     * @return string An HTML tag string
     *
     * @see sfWidgetForm
     */
    public function render($name, $value = null, $attributes = array(), $errors = array()) {

        $attributes = $attributes +
                array(
                    'type' => $this->getOption('type'),
                    'name' => $name,
                    'value' => $value,
                    'id' => $this->generateId($name, $value),
                    'onclick' => 'this.select();'
        );

        if ($this->isRequired()) {
            $attributes = $attributes + array('required' => 'required');
        }
        if ($this->getPattern()) {
            $attributes = $attributes + array('pattern' => $this->getPattern());
        }


        return $this->renderTag('input', $attributes);
    }

}